package com.xxxx.yoyo.adminweb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.IUserService;
import com.xxxx.yoyo.service.impl.UserServiceImpl;

/**
 * 修改客户账号信息
 * @author cai cai
 *
 */
@WebServlet("/admin/userEdit")
public class UserEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IUserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前端传递过来的参数id
		String userIdstr = request.getParameter("id");
		int userId = Integer.parseInt(userIdstr);
		//根据用户id获取用户信息
		User user = userService.getUserById(userId);
		//把user存入request中，供/admin/user edit.jsp页面使用
		request.setAttribute("user", user);
		
		
		//设置导航条上的[客户管理]处于选中状态
		request.setAttribute("flag",2);
		
		//请求转发，跳转到/admin/user_edit.jsp
		request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式为UTF-8,解决中文参数乱码
		request.setCharacterEncoding ("UTF-8");
		//设置导航条上的[客户管理]处于选中状态
		request.setAttribute("flag",2);
		
		//获取前端传递过来的请求参数：id,name,phone,address
		String userIdstr = request.getParameter("id");
		int userId = Integer.parseInt (userIdstr);
		//根据用户id查询用户信息
		User user = userService.getUserById(userId);
		
		String name = request.getParameter("name");
		if(name.length() > 30){//真实姓名大于30个字
			//提示错误
			request.setAttribute("msg","添加失败！真是姓名不能超过30个字符！");
			//把user存入request中，供/admin/user_edit.jsp页面使用
			request.setAttribute("user", user);
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
			return;
		}
		
		//获取手机号
		String phone = request.getParameter ("phone");
		try {
			long phoneLong = Long.parseLong(phone);
		}catch(Exception e){//出错说明，phone不全是数字
			//提示错误
			request.setAttribute("msg","添加失败！手机号有误！");
			//把user存入request中，供/admin/user_edit.jsp页面使用
			request.setAttribute("user", user);
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
			return;
		}
		
		//获取地址
		String address = request.getParameter("address");
		if(address.length() > 100){//收货地址大于了100个字
			//提示错误
			request.setAttribute("msg","添加失败！详细地址不能大于100个字！");
			//把user存入request中，供/admin/user_edit.jsp页面使用
			request.setAttribute("user", user);
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
			return;
		}
		
		//能到这里说明用户输入的信息都符合要求，开始添加
		//修改前端传过来user的信息
		user.setName(name);
		user.setPhone(phone);
		user.setAddress(address);
		
		//更新数据库中user信息
		boolean isSuccess = userService.updateUser(user);
		if(isSuccess){//更新成功
			//重定向到客户管理列表页面
			response.sendRedirect ("/YoyoShop/admin/userList?page=1&size=16");
		}else {
			//提示错误
			request.setAttribute("msg","添加失败！");
			//把user存入request中，供/admin/user_edit.jsp页面使用
			request.setAttribute("user", user);
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
		}
		
		
	}

}
